﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using coreapi.MdelsWang;
using LinCms.Core.Entities;

namespace coreapi.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v8")]
    public class RoomInformationController : ControllerBase
    {
        private readonly IFreeSql db;
        public RoomInformationController(IFreeSql _db)
        {
            this.db = _db;
        }
        /// <summary>
        /// 查询包间数据
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet("GetData")]
        public object GetData(int currentPage = 1, int pageSize = 10, string? search = "")
        {
            int currentPages = currentPage; // 当前页码
            int pageSizes = pageSize; // 每页显示的条目数
            var query = db.Select<Room, RoomInformation>()
                      .InnerJoin((a, b) => a.RoomID == b.RoomID)
                      .WhereIf(!string.IsNullOrEmpty(search), (a, b) => a.RoomName.Contains(search))
                      //匿名函数不能有多个名字相同的函数 为了避免重复aRommId = a.RoomID,
                      .ToList((a, b) => new {aRommId = a.RoomID, a.RoomName, a.Capacity, a.Description, b.InformationID, b.RoomID, b.Price, b.CanBook });
            int totalCount = (int)query.Count(); // 总条目数
            var list = query.Skip((currentPages - 1) * pageSize).Take(pageSize).ToList();

            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 添加包间数据
        /// </summary>
        /// <param name="RoomID"></param>
        /// <returns></returns>
        [HttpPost]
        public int Post(RoomInfo roomInfo)
        {
            var room = new Room()
            {
                RoomName = roomInfo.RoomName,
                Capacity = roomInfo.Capacity,
                Description = roomInfo.Description,
            };
            db.Insert(room).ExecuteAffrows();

            int maxId = db.Select<Room>().Max(b => b.RoomID);

            var roomInformation = new RoomInformation()
            {
                RoomID = maxId,
                Price = roomInfo.Price,
                CanBook = roomInfo.CanBook,
            };
            var affectedRows = db.Insert(roomInformation).ExecuteAffrows();
            return affectedRows;
        }
        /// <summary>
        /// 编辑包间信息
        /// </summary>
        /// <param name="RoomID"></param>
        /// <param name="roomInfo"></param>
        /// <returns></returns>
        [HttpPut]
        public int Put(int RoomID, RoomInfo roomInfo)
        {
            var beverage1 = db.Update<Room>().Set(a => new Room
            {
                RoomName = roomInfo.RoomName,
                Capacity = roomInfo.Capacity,
                Description = roomInfo.Description
            }).Where(a => a.RoomID == RoomID).ExecuteAffrows();

            var beverage2 = db.Update<RoomInformation>().Set(a => new RoomInformation
            {
                Price = roomInfo.Price,
                CanBook = roomInfo.CanBook,//TRUSE/FLASE 状态码
            }).Where(a => a.RoomID == RoomID).ExecuteAffrows();

            return beverage2;
        }
        /// <summary>
        /// 删除包间数据
        /// </summary>
        /// <param name="RoomID"></param>
        /// <returns></returns>
        [HttpDelete]
        public int Delete(int RoomID)
        {
            var beverage2 = db.Delete<Room>()
                                .Where(a => a.RoomID == RoomID)
                                .ExecuteAffrows();
            var beverage1 = db.Delete<RoomInformation>()
                                .Where(a => a.RoomID == RoomID)
                                .ExecuteAffrows();
            return beverage1;
        }
    }
}
